<HTML>
<HEAD>
<TITLE>swap()</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Rogue Wave Standard Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="strstreambuf.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="swap-ranges.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Rogue Wave C++ Standard Library Reference Guide</B></DIV>
<H2>swap()</H2>
<P><B>Module:</B>&nbsp;&nbsp;Standard C++ Library&nbsp;&nbsp;&nbsp;<B>Library:</B>&nbsp;&nbsp;<A HREF="2-9.html">Algorithms</A></P>

<PRE><HR><B><I>Function</I></B><HR></PRE>

<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Example</A></LI>
<LI><A HREF="#sec6">See Also</A></LI>
<LI><A HREF="#sec7">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
No Entries
<A NAME="sec2"><H3>Summary</H3></A>
<P>Algorithm that exchanges values</P>
<A NAME="sec3"><H3>Synopsis</H3></A>

<PRE>#include &lt;algorithm&gt;

namespace std {
  template &lt;class T&gt;
  void swap(T&amp; a, T&amp; b)
}
</PRE>
<A NAME="sec4"><H3>Description</H3></A>
<P>The <SAMP>swap()</SAMP> algorithm exchanges the values of <SAMP>a</SAMP> and <SAMP>b</SAMP>. The effect is:</P>

<UL><PRE>T tmp = a
a = b
b = tmp
</PRE></UL>
<A NAME="sec5"><H3>Example</H3></A>

<UL><PRE>//
//  swap.cpp
//

#include &lt;algorithm&gt;   // for copy, iter_swap
#include &lt;iostream&gt;    // for cout, endl
#include &lt;iterator&gt;    // for ostream_iterator
#include &lt;vector&gt;      // for vector

int main ()
{
    typedef std::ostream_iterator&lt;int, char, 
                                  std::char_traits&lt;char&gt; &gt; Iter;

    const int a[] = { 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 };

    // Set up a vector
    std::vector&lt;int, std::allocator&lt;int&gt; &gt; 
                   v (a, a + sizeof a / sizeof *a);

    // Output original vector.
    std::cout &lt;&lt; "For the vector: ";
    std::copy (v.begin (), v.end (), Iter (std::cout, " "));

    // Swap the first five elements with the last five elements
    std::swap_ranges (v.begin (), v.begin () + 5, 
                      v.begin () + 5);

    // Output result
    std::cout &lt;&lt; "\n\nSwapping the first five elements "
              &lt;&lt; "with the last five gives: \n     ";

    std::copy (v.begin (), v.end (), Iter (std::cout," "));

    // Now an example of iter_swap -- swap first 
    // and last elements.
    std::iter_swap (v.begin(), v.end () - 1);

    // Output result
    std::cout &lt;&lt; "\n\nSwapping the first and last elements "
              &lt;&lt; "gives: \n     ";
    std::copy (v.begin (), v.end (), Iter (std::cout, " "));

    std::cout &lt;&lt; std::endl;

    return 0;
}


Program Output:
For the vector: 6 7 8 9 10 1 2 3 4 5 

Swapping the first five elements with the last five gives: 
     1 2 3 4 5 6 7 8 9 10 

Swapping the first and last elements gives: 
     10 2 3 4 5 6 7 8 9 1 

</PRE></UL>
<A NAME="sec6"><H3>See Also</H3></A>
<P><SAMP><A HREF="iter-swap.html">iter_swap()</A></SAMP>, <SAMP><A HREF="swap-ranges.html">swap_ranges()</A></SAMP></P>
<A NAME="sec7"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.2.2</I></P>

<BR>
<HR>
<A HREF="strstreambuf.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="swap-ranges.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A></BODY>
</HTML>
